package com.eastfair.security.util;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.cache.projectcore.DictionaryCacheKeyBuilder;
import com.eastfair.cache.repository.CachePlusOps;
import com.eastfair.core.base.R;
import com.eastfair.core.model.CacheHashKey;
import com.eastfair.projectcore.api.DictionaryFeign;
import com.eastfair.projectcore.entity.Dictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DistionaryUtil {

    @Autowired
    private CachePlusOps cachePlusOps;

    @Autowired
    DictionaryFeign dictionaryFeign;


    public String getNameOfDict(String code, String type) {
        CacheHashKey typeKey = new DictionaryCacheKeyBuilder().hashFieldKey(type, Dictionary.class.getName());
        List<Dictionary> objectList = cachePlusOps.hGet(typeKey);
        if (objectList == null || objectList.isEmpty()) {
            R<List<Dictionary>> listR = dictionaryFeign.queryDict(type);
            if (listR.getIsSuccess()) {
                objectList = listR.getData();
            }
        }
        if (CollectionUtil.isEmpty(objectList)) {
            return null;
        }
        for (Dictionary dictionary1 : objectList) {
            if (dictionary1.getCode().equals(code)) {
                return dictionary1.getName();
            }
        }
        return null;
    }

}